import pymongo
from config.configs import ALPHA_DB_HOST, ALPHA_DB_PORT
from tools.funciton import spuId_to_articleNumber, skuId_to_size, write_xlsx

alpha = pymongo.MongoClient(ALPHA_DB_HOST, ALPHA_DB_PORT).alpha
jw = pymongo.MongoClient(ALPHA_DB_HOST, ALPHA_DB_PORT).jiwu


def flash_flaw_return_export(db, cond, filename):
    """闪电质检退回签收记录导出"""
    datas = []
    if db == 'alpha':
        pipeline = [
            {
                '$match': cond
            },
            {
                '$project':
                    {
                        '_id': 0,
                        'merchantCode': 1,
                        'merchantName': 1,
                        'spuId': 1,
                        'skuId': 1,
                        'failReason': 1,
                        'uniqueCode': 1
                    }
            }
        ]
        [datas.append([i['merchantCode'], i['merchantName'], spuId_to_articleNumber('alpha', i['spuId']),
                       skuId_to_size('alpha', i['skuId']),
                       i['failReason'], i['uniqueCode']]) for i in alpha.flash_flaw_return.aggregate(pipeline=pipeline)]
        write_xlsx(['商家编号', '商家名称', '货号', '尺码', '瑕疵原因', '唯一码'], datas, filename)
    elif db == 'jiwu':
        pipeline = [
            {
                '$match': cond
            },
            {
                '$project':
                    {
                        '_id': 0,
                        'merchantCode': 1,
                        'merchantName': 1,
                        'spuId': 1,
                        'skuId': 1,
                        'failReason': 1,
                        'uniqueCode': 1
                    }
            }
        ]
        [datas.append([i['merchantCode'], i['merchantName'], spuId_to_articleNumber('alpha', i['spuId']),
                       skuId_to_size('alpha', i['skuId']),
                       i['failReason'], i['uniqueCode']]) for i in jw.flash_flaw_return.aggregate(pipeline=pipeline)]
        write_xlsx(['商家编号', '商家名称', '货号', '尺码', '瑕疵原因', '唯一码'], datas, filename)



flash_flaw_return_export('jiwu', {'receivedTime': {'$gt': 1726502400000}}, '0917质检退货签收')
